using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactor_Trap_SeaAnemone_Ctrl : Unit_Interactor_Ctrl
{
    public Transform target;
    public Transform bone_2;
    public Transform headPoint;
    public bool isFree=true;
    public Sprite default_Sprite;
    public Sprite eat_Sprite;
    public GameObject default_Body;
    public GameObject collider_Body;
    public bool isEated = false;
    public IEnumerator eattingIE;

    private void Start()
    {
    }
    [ContextMenu("Init")]
    private void Init()
    {
        default_Body.gameObject.SetActive(true);
        collider_Body.gameObject.SetActive(false);
        bone_2.transform.localScale = new Vector3(1, 1, 1);
        bone_2.transform.right = bone_2.transform.parent.right;
        bone_2.transform.eulerAngles = new Vector3(0, 0, bone_2.transform.eulerAngles.z);
        target.transform.position = headPoint.transform.position;
    }

    public override void ReceiveLight(Vector3 sourcePos, Vector3 sourceDirection)
    {
        base.receiveLightEvent?.Invoke(sourcePos, sourceDirection);
    }
    private void Update()
    {

    }

    public void EatSleepGrass(Unit_Item_Ctrl item_Ctrl)
    {
        if (isEated)
        {
            return;
        }
        isEated = true;
        isFree = false;
        Vector3 tagetInitPos = target.transform.position;
        float initAngle = bone_2.transform.eulerAngles.z;
        float targetLengh = (this.transform.position - item_Ctrl.transform.position).magnitude - (this.transform.position - target.transform.position).magnitude;
        Vector3 boneInitPos = bone_2.transform.position;
        Vector3 boneTargetPos = boneInitPos + (item_Ctrl.transform.position - bone_2.transform.position).normalized * Mathf.Max(targetLengh,0);

        float targetAngle = 0;

        IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
        {
            targetAngle = Mathf.Clamp(-Vector3.SignedAngle((item_Ctrl.transform.position - this.bone_2.transform.position), this.bone_2.transform.parent.right,Vector3.forward), -45f,45f);
            //bone_2.transform.right = (unit_Item_Ctrl.transform.position - this.bone_2.transform.position).normalized;
            bone_2.transform.localEulerAngles = new Vector3(0, 0, targetAngle);
            target.transform.position = Vector3.Lerp(tagetInitPos, item_Ctrl.transform.position, 1 - _time / 0.32f);
            bone_2.transform.position = Vector3.Lerp(boneInitPos, boneTargetPos, 1 - _time / 0.32f);
            return true;
        }, () =>
        {
            item_Ctrl.gameObject.SetActive(false);
            renderer.sprite = eat_Sprite;
            IEPool_Manager.instance.WaitTimeToDo(ref eattingIE, 0.16f, null, () =>
            {
                IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
                {
                    bone_2.transform.right = bone_2.transform.parent.right;
                    bone_2.transform.eulerAngles = new Vector3(0, 0, bone_2.transform.eulerAngles.z);
                    bone_2.transform.position = Vector3.Lerp(boneTargetPos,boneInitPos, 1 - _time / 0.32f);
                    target.transform.position = Vector3.Lerp(item_Ctrl.transform.position, headPoint.transform.position, 1 - _time / 0.32f);
                    return true;
                }, () =>
                {
                    default_Body.gameObject.SetActive(false);
                    collider_Body.gameObject.SetActive(true);
                });
            });
          
        });
    }
    public void EatCharactor(Unit_Charactor_Ctrl charactor_Ctrl)
    {
        if (isEated)
        {
            return;
        }
        isEated = true;
        isFree = false;
        Vector3 tagetInitPos = target.transform.position;
        float initAngle = bone_2.transform.eulerAngles.z;
        float targetLengh = (this.transform.position - charactor_Ctrl.transform.position).magnitude - (this.transform.position - target.transform.position).magnitude;
        Vector3 boneInitPos = bone_2.transform.position;
        Vector3 boneTargetPos = boneInitPos + (charactor_Ctrl.transform.position - bone_2.transform.position).normalized * Mathf.Max(targetLengh, 0);

        float targetAngle = 0;

        IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
        {
            targetAngle = Mathf.Clamp(-Vector3.SignedAngle((charactor_Ctrl.transform.position - this.bone_2.transform.position), this.bone_2.transform.parent.right, Vector3.forward), -45f, 45f);
            //bone_2.transform.right = (unit_Item_Ctrl.transform.position - this.bone_2.transform.position).normalized;
            bone_2.transform.localEulerAngles = new Vector3(0, 0, targetAngle);
            target.transform.position = Vector3.Lerp(tagetInitPos, charactor_Ctrl.transform.position, 1 - _time / 0.32f);
            bone_2.transform.position = Vector3.Lerp(boneInitPos, boneTargetPos, 1 - _time / 0.32f);
            return true;
        }, () =>
        {
            charactor_Ctrl.moveLocks.Add("Death");
            charactor_Ctrl.gameObject.SetActive(false);
            renderer.sprite = eat_Sprite;
            IEPool_Manager.instance.WaitTimeToDo(ref eattingIE, 0.16f, null, () =>
            {
                IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
                {
                    bone_2.transform.right = bone_2.transform.parent.right;
                    bone_2.transform.eulerAngles = new Vector3(0, 0, bone_2.transform.eulerAngles.z);
                    bone_2.transform.position = Vector3.Lerp(boneTargetPos, boneInitPos, 1 - _time / 0.32f);
                    target.transform.position = Vector3.Lerp(charactor_Ctrl.transform.position, headPoint.transform.position, 1 - _time / 0.32f);
                    return true;
                }, () =>
                {
                    default_Body.gameObject.SetActive(false);
                    collider_Body.gameObject.SetActive(true);
                    UIManager.instance.SwtichMaskPerformance(true, 1, () => {
                        LoadScene_Manager.instance.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
                    });
                });
            });
        });
    }
    public void EatOther(Unit_Biology_Ctrl biology_Ctrl)
    {
        if (isEated)
        {
            return;
        }
        isEated = true;
        isFree = false;
        Vector3 tagetInitPos = target.transform.position;
        float initAngle = bone_2.transform.eulerAngles.z;
        float targetLengh = (this.transform.position - biology_Ctrl.transform.position).magnitude - (this.transform.position - target.transform.position).magnitude;
        Vector3 boneInitPos = bone_2.transform.position;
        Vector3 boneTargetPos = boneInitPos + (biology_Ctrl.transform.position - bone_2.transform.position).normalized * Mathf.Max(targetLengh, 0);

        float targetAngle = 0;

        IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
        {
            targetAngle = Mathf.Clamp(-Vector3.SignedAngle((biology_Ctrl.transform.position - this.bone_2.transform.position), this.bone_2.transform.parent.right, Vector3.forward), -45f, 45f);
            //bone_2.transform.right = (unit_Item_Ctrl.transform.position - this.bone_2.transform.position).normalized;
            bone_2.transform.localEulerAngles = new Vector3(0, 0, targetAngle);
            target.transform.position = Vector3.Lerp(tagetInitPos, biology_Ctrl.transform.position, 1 - _time / 0.32f);
            bone_2.transform.position = Vector3.Lerp(boneInitPos, boneTargetPos, 1 - _time / 0.32f);
            return true;
        }, () =>
        {
            biology_Ctrl.gameObject.SetActive(false);
            renderer.sprite = eat_Sprite;
            IEPool_Manager.instance.WaitTimeToDo(ref eattingIE, 0.16f, null, () =>
            {
                IEPool_Manager.instance.KeepTimeToDo(ref eattingIE, 0.32f, null, (_time) =>
                {
                    bone_2.transform.right = bone_2.transform.parent.right;
                    bone_2.transform.eulerAngles = new Vector3(0, 0, bone_2.transform.eulerAngles.z);
                    bone_2.transform.position = Vector3.Lerp(boneTargetPos, boneInitPos, 1 - _time / 0.32f);
                    target.transform.position = Vector3.Lerp(biology_Ctrl.transform.position, headPoint.transform.position, 1 - _time / 0.32f);
                    return true;
                }, () =>
                {
                    default_Body.gameObject.SetActive(false);
                    collider_Body.gameObject.SetActive(true);
                });
            });
        });
    }
}
